package com.lzy.shops.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lzy.shops.bean.PageBean;
import com.lzy.shops.bean.Search;
import com.lzy.shops.service.GoodsCarService;
import com.lzy.shops.utils.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/goodsCar")
public class GoodsCarController {

    @Autowired
    private GoodsCarService service;

    private ObjectMapper objectMapper = new ObjectMapper();

    @GetMapping("/findAll")
    public String findAll(@RequestParam Map<String,Object> maps) throws JsonProcessingException {
        ResponseData<Object> responseData = null;
        PageBean all = null;
        try {
            String userId = (String) maps.get("userId");
            String pageNo = (String) maps.get("pageNo");
            String pageSize = (String) maps.get("pageSize");
            String search = (String) maps.get("search");
            Integer end = Integer.parseInt(pageSize);
            Integer start = (Integer.parseInt(pageNo)-1)*end;
            HashMap<String,String> hashMap = JSON.parseObject(search, HashMap.class);
            Search searchBean = new Search(start,end,hashMap);
            all = service.findAll(Integer.parseInt(userId),searchBean);
            all.setPageNo(Integer.parseInt(pageNo));
            all.setPageSize(Integer.parseInt(pageSize));
            responseData = new ResponseData<Object>(true,all);
        } catch (Exception e) {
            responseData = new ResponseData<Object>(false,e.getMessage());
        }
        return objectMapper.writeValueAsString(responseData);
    }

    @GetMapping("/addGoodsCar")
    public String addGoodsCar(@RequestParam Map<String,Object> maps) throws JsonProcessingException {
        ResponseData<Object> responseData = null;
        try {
            String shopId = (String) maps.get("shopId");
            String nums = (String) maps.get("nums");
            String userId = (String) maps.get("userId");
            Integer count = service.addGoodsCar(Integer.parseInt(shopId), Integer.parseInt(nums), Integer.parseInt(userId));
            if(count!=0){
                responseData = new ResponseData<Object>(true,"添加成功");
            }else throw new RuntimeException("数据异常");
        } catch (Exception e) {
            responseData = new ResponseData<Object>(false,e.getMessage());
        }
        return objectMapper.writeValueAsString(responseData);
    }

    @GetMapping("/findByGoodsCar")
    public String findByGoodsCar(@RequestParam Map<String,Object> maps) throws JsonProcessingException {
        ResponseData<Object> responseData = null;
        try {
            String shopId = (String) maps.get("shopId");
            String userId = (String) maps.get("userId");
            Integer count = service.findByGoodsCarNums(Integer.parseInt(shopId),Integer.parseInt(userId));
            if(count==0){
                responseData = new ResponseData<Object>(true,"");
            }else throw new RuntimeException();
        } catch (Exception e) {
            responseData = new ResponseData<Object>(false,"");
        }
        return objectMapper.writeValueAsString(responseData);
    }

    @PutMapping("/updateGoodsCarNums")
    public String updateGoodsCarNums(@RequestBody Map<String,Object> maps) throws JsonProcessingException {
        ResponseData<Object> responseData = null;
        try {
            String shopId = (String) maps.get("carId");
            String userId = (String) maps.get("userId");
            String nums = (String) maps.get("nums");
            Boolean bool = service.updateGoodsCarNums(Integer.parseInt(shopId), Integer.parseInt(userId),Integer.parseInt(nums));
            if(bool){
                responseData = new ResponseData<Object>(true,"");
            }else throw new RuntimeException();
        } catch (Exception e) {
            responseData = new ResponseData<Object>(false,"");
        }
        return objectMapper.writeValueAsString(responseData);
    }

    @DeleteMapping("/deleteGoodsCars")
    public String deleteGoodsCars(@RequestParam String ids) throws JsonProcessingException {
        ResponseData<Object> responseData = null;
        try {
            JSONObject jsonObject = JSON.parseObject(ids);
            Map map = JSONObject.toJavaObject(jsonObject, Map.class);
            String userId = (String) map.get("u");
            String carId = (String) map.get("c");
            Integer count = service.deleteGoodsCars(userId, carId);
            if(count>=1){
                responseData = new ResponseData<Object>(true,"成功删除"+count+"件商品");
            }else throw new RuntimeException("数据异常");
        } catch (Exception e) {
            responseData = new ResponseData<Object>(false,e.getMessage());
        }
        return objectMapper.writeValueAsString(responseData);
    }

    @DeleteMapping("/deleteGoodsCar/{userId}/{carId}")
    public String deleteGoodsCar(@PathVariable("userId") Integer userId,@PathVariable("carId") Integer carId) throws JsonProcessingException {
        ResponseData<Object> responseData = null;
        try {
            Integer integer = service.deleteGoodsCar(userId, carId);
            if(integer==1){
                responseData = new ResponseData<Object>(true,"删除成功");
            }else throw new RuntimeException("数据异常");
        } catch (Exception e) {
            responseData = new ResponseData<Object>(false,e.getMessage());
        }
        return objectMapper.writeValueAsString(responseData);
    }
}
